package com.mall.server.service.pms;


import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.dubbo.api.PmsProductCategoryApi;
import com.mall.model.domain.PmsProductCategory;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.ProductCategoryWithChildrenVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PmsProductCategoryService {

    @DubboReference
    private PmsProductCategoryApi productCategoryApi;

    public void addProductCategory(PmsProductCategory productCategory) {
        PmsProductCategory category=  productCategoryApi.findByName(productCategory.getName());
        if (category==null){
            productCategoryApi.saveCategory(productCategory);
        }
    }

    public void delProductCategory(Long id) {
        productCategoryApi.deleteCategory(id);
    }

    public CommonPage<PmsProductCategory> findParentId(Long parentId, Integer pageNum, Integer pageSize) {
        Page<PmsProductCategory> categories = productCategoryApi.findParentId(parentId,pageNum,pageSize);
        CommonPage<PmsProductCategory> pageInfo = new CommonPage<>(categories);

        return pageInfo;
    }

    public void updateNavStatus(Long[] ids, Integer navStatus) {
        List<PmsProductCategory> list=productCategoryApi.findByIds(ids);
        for (PmsProductCategory category : list) {
        if (category!=null){
            category.setNavStatus(navStatus);

            productCategoryApi.updateCategory(category);
            }
        }
    }

    public void updateShowStatus(Long[] ids, Integer showStatus) {
        List<PmsProductCategory> list=productCategoryApi.findByIds(ids);
        for (PmsProductCategory category : list) {
            if (category!=null){
                category.setShowStatus(showStatus);

                productCategoryApi.updateCategory(category);
            }
        }
    }

    public void updateById(Integer id, PmsProductCategory productCategory) {
        productCategory.setId(Convert.toLong(id));
        productCategoryApi.updateById(productCategory);
    }

    public PmsProductCategory selectById(Integer id) {
        PmsProductCategory category= productCategoryApi.findById(id);
        return  category;
    }

    public List<ProductCategoryWithChildrenVo> selectListWithChildren() {
        List<ProductCategoryWithChildrenVo> vos = new ArrayList<>();
        List<PmsProductCategory>  categorys= productCategoryApi.findByParentId(0L);
        for (PmsProductCategory category : categorys) {
            if (category.getParentId()==0){
                List<PmsProductCategory> childrenList=productCategoryApi.findByParentId(category.getId());
                if (childrenList.size()>0){
                    ProductCategoryWithChildrenVo vo = ProductCategoryWithChildrenVo.init(childrenList, category);
                    vos.add(vo);
                }
            }
        }
        return vos;
    }
}
